class Solution
{
public:
    int maxStarSum(vector<int> &vals, vector<vector<int>> &edges, int k)
    {
        if (k == 0)
        {
            return *max_element(vals.begin(), vals.end());
        }
        int n = vals.size();
        vector<vector<int>> adjVals(n);
        for (auto &edge : edges)
        {
            adjVals[edge[0]].push_back(vals[edge[1]]);
            adjVals[edge[1]].push_back(vals[edge[0]]);
        }
        int maxSum = INT_MIN;
        for (int i = 0; i < n; ++i)
        {
            sort(adjVals[i].rbegin(), adjVals[i].rend());
            int size = adjVals[i].size();
            int sum = 0;
            for (int j = 0; j < min(k, size); ++j)
            {
                if (adjVals[i][j] <= 0)
                {
                    break;
                }
                sum += adjVals[i][j];
            }
            sum += vals[i];
            maxSum = max(sum, maxSum);
        }
        return maxSum;
    }
};